package com.example.backend.mapper;

import com.example.backend.entity.DingDanDetail;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Mapper
@Repository
public interface DingDanDetailMapper {


    @Insert("insert into DingDan_detail(dingdan_num,dish_name,num,price) values(#{dingdan_num},#{dish_name},#{num},#{price})")
    int insert(DingDanDetail dingDanDetail);


    @Delete("delete from dingdan_detail where dingdan_num=#{dingdan_num}")
    int deleteByDingDanNum(String dingdan_num);


    @Select("select * from dingdan_detail ")
    ArrayList<DingDanDetail> selectDingDanDetails();



    /**
     * 根据订单编号获取订单详情（菜品信息）
     */
    @Select("SELECT * FROM dingdan_detail WHERE dingdan_num = #{dingdan_num}")
    List<DingDanDetail> selectDingDanDetailsByDingDanNum(String dingdan_num);

    @SelectProvider(type = DingDanDetailMapperProvider.class, method = "countDishSalesByOrderNumbersSQL")
    List<Map<String, Object>> countDishSalesByOrderNumbers(List<String> orderNumbers);

    /**
     * 计算总收入
     *
     * @param orderNumbers 订单编号列表
     * @return 总收入
     */
    @SelectProvider(type = DingDanDetailMapperProvider.class, method = "calculateTotalIncomeByOrderNumbersSQL")
    List<Map<String, Object>> calculateTotalIncomeByOrderNumbers(List<String> orderNumbers);

    // 添加在接口中
    @Select("SELECT SUM(price * num) as total_sales FROM dingdan_detail")
    double calculateTotalSales();

    @Select("SELECT SUM(price * num) as total_sales, DATE_FORMAT(d.time, '%Y-%m') as month FROM dingdan_detail dd JOIN dingdan d ON dd.dingdan_num = d.dingdan_num WHERE d.state = '5' GROUP BY month ORDER BY month")
    List<Map<String, Object>> getSalesTrend();
}